var wp_surveys_navigation_data = {
	question_count : null, 
	current_question : 0,
	next_label : '',
	previous_label : '',
	finish_label : '',
	no_answer_text : ''
};

jQuery(document).ready(function() {
	wp_surveys_navigation_data.question_count = parseInt(jQuery('#question_count').val());
//	wp_surveys_navigation_data.next_label = jQuery('#wp_surveys_navigate_next').val();
//	wp_surveys_navigation_data.previous_label = jQuery('#wp_surveys_navigate_previous').val();
//	wp_surveys_navigation_data.finish_label = jQuery('#wp_surveys_navigate_finish').val();
	
	jQuery('.wp_survey_submit_div').after(wp_surveys_step_navigation_HTML.join(''));
	jQuery('.wp_survey_submit_div, .wp_survey_question_info').css('display', 'none');
	jQuery('.wp_surveys_navigate_previous').attr('disabled', 'disabled');
});

function wp_surveys_next_step() {
	if (wp_surveys_navigation_data.current_question > 0) {
		var question_id = jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question+'_id').val();
		var answers = jQuery('.answer_'+question_id);
		var answered = false;
		for (var i = 0; i < answers.length; i++) {
			if (jQuery(answers[i]).attr('checked')) {
				answered = true;
				break;
			}	
		}
		if (!answered) {
			jQuery('.wp_survey_messages').html(wp_surveys_navigation_data.no_answer_text).css('display', 'block');
			return;
		}
	}
	jQuery('.wp_survey_messages').css('display', 'none');
	
	if (wp_surveys_navigation_data.current_question == 0) {
		wp_surveys_navigation_data.current_question++;
		jQuery('.wp_survey_info').css('display', 'none');
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'block');
	} else if (wp_surveys_navigation_data.current_question < wp_surveys_navigation_data.question_count) {
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'none');
		wp_surveys_navigation_data.current_question ++;
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'block');
	} else {
		if (confirm('You have reached the last question, would you like to submit your survey now?')){
			jQuery('.wp_survey_submit_div, .wp_survey_question_info, .wp_survey_submit_div').css('display', 'block');
			jQuery('#wp_survey_form').submit();
			jQuery('.wp_survey_submit_div, .wp_survey_question_info, .wp_survey_submit_div').css('display', 'none');
			jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'block');
		}
	}

	if (wp_surveys_navigation_data.current_question == 0) {
		jQuery('.wp_surveys_navigate_previous').attr('disabled', 'disabled');
	} else {
		jQuery('.wp_surveys_navigate_previous').removeAttr('disabled');
	}
	
	if (wp_surveys_navigation_data.current_question == wp_surveys_navigation_data.question_count) {
		jQuery('.wp_surveys_navigate_next').val(wp_surveys_navigation_data.finish_label);
	} else {
		jQuery('.wp_surveys_navigate_next').val(wp_surveys_navigation_data.next_label);
	}
	
}

function wp_surveys_previous_step() {
	jQuery('.wp_survey_messages').css('display', 'none');

	if (wp_surveys_navigation_data.current_question == 1) {
		jQuery('.wp_survey_info').css('display', 'block');
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'none');
		wp_surveys_navigation_data.current_question--;
	} else if (wp_surveys_navigation_data.current_question > 0) {
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'none');
		wp_surveys_navigation_data.current_question --;
		jQuery('#wp_survey_question_'+wp_surveys_navigation_data.current_question).css('display', 'block');
	}
	
	if (wp_surveys_navigation_data.current_question == 0) {
		jQuery('.wp_surveys_navigate_previous').attr('disabled', 'disabled');
	} else {
		jQuery('.wp_surveys_navigate_previous').removeAttr('disabled');
	}
	jQuery('.wp_surveys_navigate_next').val(wp_surveys_navigation_data.next_label);
}